package com.orion.schdule;

import com.orion.util.SmallTool;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * spring的schedule线程池默认只有一个线程,现在设置默认的异步线程池（默认有8个线程）
 * <p>
 * 多个定时任务指定间隔执行
 *
 * @author Administrator
 */
@Component
@ConditionalOnProperty(value = "schedule.multiScheduleWithDefaultAsyncPoolTask", havingValue = "true")
public class MultiScheduleWithDefaultAsyncPoolTask {

    private AtomicInteger atomicIntegerForA = new AtomicInteger(1);
    private AtomicInteger atomicIntegerForB = new AtomicInteger(1);

    /**
     * 每隔2s执行一次
     */
    @Async
    @Scheduled(cron = "0/2 * * * * ? ")
    public void JustScheduleForA() {
        int i = atomicIntegerForA.getAndIncrement();
        SmallTool.printTimeAndThread("begin Just Schedule Task A " + i);
        SmallTool.sleepMillis(4000);
        SmallTool.printTimeAndThread("end Just Schedule Task A " + i + " -------");
    }

    /**
     * 每隔2s执行一次
     */
    @Async
    @Scheduled(cron = "0/2 * * * * ? ")
    public void JustScheduleForB() {
        int i = atomicIntegerForB.getAndIncrement();
        SmallTool.printTimeAndThread("begin Just Schedule Task B " + i);
        SmallTool.sleepMillis(4000);
        SmallTool.printTimeAndThread("end Just Schedule Task B " + i + " -------");
    }

    /**
     * A、B都没有睡眠的时候，A、B互不影响
     *
     1644744438017	|	35	|	task-1	|	begin Just Schedule Task B 1
     1644744438017	|	36	|	task-2	|	begin Just Schedule Task A 1
     1644744440012	|	37	|	task-3	|	begin Just Schedule Task B 2
     1644744440012	|	38	|	task-4	|	begin Just Schedule Task A 2
     1644744442010	|	39	|	task-5	|	begin Just Schedule Task B 3
     1644744442010	|	40	|	task-6	|	begin Just Schedule Task A 3
     1644744444012	|	41	|	task-7	|	begin Just Schedule Task B 4
     1644744444012	|	42	|	task-8	|	begin Just Schedule Task A 4
     1644744446011	|	35	|	task-1	|	begin Just Schedule Task B 5
     1644744446011	|	36	|	task-2	|	begin Just Schedule Task A 5
     1644744448009	|	37	|	task-3	|	begin Just Schedule Task B 6
     1644744448009	|	38	|	task-4	|	begin Just Schedule Task A 6

     B睡秒4s，超过间隔时间，A、B互不影响，就连A自己内部多次执行都不会影响自己重复的任务（换A睡眠也是同理）
     1644744028027	|	35	|	task-1	|	begin Just Schedule Task B 1
     1644744028027	|	36	|	task-2	|	begin Just Schedule Task A 1
     1644744030010	|	37	|	task-3	|	begin Just Schedule Task B 2
     1644744030010	|	38	|	task-4	|	begin Just Schedule Task A 2
     1644744032009	|	39	|	task-5	|	begin Just Schedule Task B 3
     1644744032009	|	40	|	task-6	|	begin Just Schedule Task A 3
     1644744032040	|	35	|	task-1	|	end Just Schedule Task B 1 -------
     1644744034020	|	37	|	task-3	|	end Just Schedule Task B 2 -------
     1644744034025	|	41	|	task-7	|	begin Just Schedule Task B 4
     1644744034027	|	42	|	task-8	|	begin Just Schedule Task A 4
     1644744036001	|	36	|	task-2	|	begin Just Schedule Task A 5
     1644744036001	|	38	|	task-4	|	begin Just Schedule Task B 5
     1644744036018	|	39	|	task-5	|	end Just Schedule Task B 3 -------
     1644744038003	|	40	|	task-6	|	begin Just Schedule Task A 6
     1644744038003	|	35	|	task-1	|	begin Just Schedule Task B 6
     1644744038034	|	41	|	task-7	|	end Just Schedule Task B 4 -------

     各自都睡眠4s，默认的异步线程池有8个线程，互不影响，就连A自己内部多次执行都不会影响自己重复的任务

     1644743948031	|	36	|	task-2	|	begin Just Schedule Task B 1
     1644743948031	|	35	|	task-1	|	begin Just Schedule Task A 1
     1644743950025	|	37	|	task-3	|	begin Just Schedule Task A 2
     1644743950025	|	38	|	task-4	|	begin Just Schedule Task B 2
     1644743952006	|	39	|	task-5	|	begin Just Schedule Task A 3
     1644743952006	|	40	|	task-6	|	begin Just Schedule Task B 3
     1644743952037	|	36	|	task-2	|	end Just Schedule Task B 1 -------
     1644743952037	|	35	|	task-1	|	end Just Schedule Task A 1 -------
     1644743954009	|	41	|	task-7	|	begin Just Schedule Task A 4
     1644743954009	|	42	|	task-8	|	begin Just Schedule Task B 4
     1644743954040	|	38	|	task-4	|	end Just Schedule Task B 2 -------
     1644743954040	|	37	|	task-3	|	end Just Schedule Task A 2 -------
     1644743956009	|	40	|	task-6	|	end Just Schedule Task B 3 -------
     1644743956009	|	39	|	task-5	|	end Just Schedule Task A 3 -------
     1644743956009	|	36	|	task-2	|	begin Just Schedule Task A 5
     1644743956009	|	35	|	task-1	|	begin Just Schedule Task B 5
     1644743958006	|	38	|	task-4	|	begin Just Schedule Task A 6
     1644743958006	|	37	|	task-3	|	begin Just Schedule Task B 6
     1644743958021	|	42	|	task-8	|	end Just Schedule Task B 4 -------
     1644743958021	|	41	|	task-7	|	end Just Schedule Task A 4 -------
     1644743960006	|	40	|	task-6	|	begin Just Schedule Task A 7
     1644743960006	|	39	|	task-5	|	begin Just Schedule Task B 7
     1644743960021	|	35	|	task-1	|	end Just Schedule Task B 5 -------
     1644743960021	|	36	|	task-2	|	end Just Schedule Task A 5 -------
     1644743962016	|	37	|	task-3	|	end Just Schedule Task B 6 -------
     1644743962016	|	38	|	task-4	|	end Just Schedule Task A 6 -------

     */
}
